<?php

namespace app\controller;

use app\model\TestModel;
use think\db\exception\DbException;
use think\response\Json;

class Modeltest
{
    /**
     * 测试插入数据
     * @return Json
     */
    public function testInsert(): Json
    {
        $data = [
            'name' => 'test',
            'age' => 18,
            'sex' => 0,
        ];
        $model = TestModel::create($data);
        $sql = TestModel::getLastSql();
        return json(['model' => $model, 'sql' => $sql]);
    }

    /**
     * 测试插入数据
     * @return Json
     */
    public function testInsertForDefaultData()
    {
        $model = TestModel::create([
            'name' => 'test',
        ]);
        $sql = TestModel::getLastSql();
        return json(['model' => $model, 'sql' => $sql]);
    }

    /**
     * 测试删除数据
     * @return Json
     * @throws DbException
     */
    public function testDelete()
    {
        // $res = TestModel::where('age', '=', 18)->delete(); // 这样是不对的
        $res = TestModel::where('_id', '=', '65cd948ab0e8da2f6d032333')->find()->delete();
        $sql = TestModel::getLastSql();
        return json(['res' => $res, 'sql' => $sql]);
    }

    /**
     * 测试删除数据
     * @return Json
     */
    public function testDestroy()
    {
        // destroy 的好处是，不需要担心是否已经删除了，删就完事了
        $res = TestModel::destroy(['65cd948ab0e8da2f6d032333', '65cd949d7e595593cc06a5c4']);
        $sql = TestModel::getLastSql();
        return json(['res' => $res, 'sql' => $sql]);
    }

    /**
     * 测试更新数据
     * @return Json
     * @throws DbException
     */
    public function testUpdate(): Json
    {
        // 先调用 find 之后，才会自动更新 update_time 字段
        $res = TestModel::where('_id', '=', '65cd954dcbc6443eab022d63')->find()->save([
            'age' => 28,
        ]);
        $sql = TestModel::getLastSql();
        return json(['res' => $res, 'sql' => $sql]);
    }

    /**
     * 测试查询数据
     * @return Json
     * @throws DbException
     */
    public function testFind(): Json
    {
        $model = TestModel::where('_id', '=', '65cd954dcbc6443eab022d63')->find();
        $sql = TestModel::getLastSql();
        return json(['model' => $model, 'sql' => $sql]);
    }

    /**
     * 测试查询数据
     * @return Json
     * @throws DbException
     */
    public function testSelect(): Json
    {
        $model = TestModel::where('age', '=', 18)->select();
        $sql = TestModel::getLastSql();
        return json(['model' => $model, 'sql' => $sql]);
    }

    /**
     * 测试count数据
     * @return Json
     * @throws DbException
     */
    public function testCount(): Json
    {
        $res = TestModel::where('age', '=', 18)->count();
        $sql = TestModel::getLastSql();
        return json(['res' => $res, 'sql' => $sql]);
    }

    /**
     * 测试分页数据
     * @return Json
     * @throws DbException
     */
    public function testPage(): Json
    {
        $res = TestModel::where('age', '=', 18)->page(1, 10)->select();
        $sql = TestModel::getLastSql();
        return json(['res' => $res, 'sql' => $sql]);
    }

    /**
     * 测试分页数据
     * @return Json
     * @throws DbException
     */
    public function testPaginate(): Json
    {
        $res = TestModel::where('age', '=', 18)
            ->order('create_time', 'asc')
            // ->order('$natural', 'desc')
            // ->order(['$natural' => 'desc', 'age' => -1])
            ->paginate(10);
        $sql = TestModel::getLastSql();
        return json(['res' => $res, 'sql' => $sql]);
    }
}